home *** CD-ROM | disk | FTP | other *** search
- Path: nntp-serv.cam.ac.uk!gdr11
- From: gdr11@cl.cam.ac.uk (Gareth Rees)
- Newsgroups: comp.lang.c
- Subject: Re: variable-lenght argument list
- Date: 18 Mar 1996 18:46:55 GMT
- Organization: University of Cambridge, England
- Message-ID: <GDR11.96Mar18184655@cl.cam.ac.uk>
- References: <4icl0o$b3j@saturn.exodus.net>
- NNTP-Posting-Host: stint.cl.cam.ac.uk
- In-reply-to: Austin Ju's message of 15 Mar 1996 20:47:20 GMT
-
- Austin Ju <jun@pixera.com> wrote:
- > Is there any way we may call a function with varable-length argument
- > list within another function also with varable-length argument list.
-
- There is no way to do this in ANSI C; see question 15.12 in the
- comp.lang.c FAQ list (ftp://rtfm.mit.edu/usenet/comp.lang.c/C-FAQ-list).
-
- 15.12: How can I write a function which takes a variable number of
- arguments and passes them to some other function (which takes a
- variable number of arguments)?
-
- A: In general, you cannot. Ideally, you should provide a version
- of that other function which accepts a va_list pointer
- (analogous to vfprintf(); see question 15.5 above). If the
- arguments must be passed directly as actual arguments, or if
- you do not have the option of rewriting the second function to
- accept a va_list (in other words, if the second, called
- function must accept a variable number of arguments, not a
- va_list), no portable solution is possible. (The problem could
- perhaps be solved by resorting to machine-specific assembly
- language; see also question 15.13 below.)
-
- I note that the GNU coding standards suggest the following dreadful
- hack, which would render your program unportable to many systems (and
- maybe even to your own system should you change compiler). But these
- may not be important considerations.
-
- In certain cases, it is ok to pass integer and pointer arguments
- indiscriminately to the same function, and use no prototype on any
- system. For example, many GNU programs have error-reporting functions
- that pass their arguments along to `printf' and friends:
-
- error (s, a1, a2, a3)
- char *s;
- int a1, a2, a3;
- {
- fprintf (stderr, "error: ");
- fprintf (stderr, s, a1, a2, a3);
- }
-
- In practice, this works on all machines, and it is much simpler than
- any "correct" alternative. Be sure *not* to use a prototype for such
- functions.
-
- --
- Gareth Rees
-